package com.example.studentsRoom.controller;


import com.example.studentsRoom.entity.DormitoryAdmin;
import com.example.studentsRoom.form.RuleForm;
import com.example.studentsRoom.form.SearchForm;
import com.example.studentsRoom.service.impl.DormitoryAdminService;
import com.example.studentsRoom.util.ResultUtil;
import com.example.studentsRoom.vo.PageVo;
import com.example.studentsRoom.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/dormitoryAdmin")
@Slf4j
public class DormitoryAdminController {

    @Autowired
    private DormitoryAdminService dormitoryAdminService;

    /**
     * 登录
     * @param ruleForm
     * @return
     */
    @GetMapping("/login")
    public ResultVo login(RuleForm ruleForm){
        ResultVo resultVo = this.dormitoryAdminService.login(ruleForm);
        log.info("宿舍管理员{{}}",resultVo.getData());
        return resultVo;
    }

    /**
     * 添加宿管
     * @param admin
     * @return
     */
    @GetMapping("/save")
    public ResultVo save(DormitoryAdmin admin){

        log.info("保存{}",admin);
        boolean save = dormitoryAdminService.save(admin);

        ResultVo resultVo=new ResultVo();
        if(save){
            resultVo.setCode(0);
        }else{
            resultVo.setCode(-1);
        }
        return resultVo;
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list/{page}/{size}")
    public ResultVo list(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        PageVo list=dormitoryAdminService.listPage(page,size);
        return ResultUtil.success(list);
    }

    /**
     * 模糊查询+分页
     * @param searchForm
     * @return
     */
    @GetMapping("/search")
    public ResultVo search(SearchForm searchForm){
        PageVo pageVo=dormitoryAdminService.search(searchForm);
        return ResultUtil.success(pageVo);
    }

    /**
     * 根据id获取信息
     * @param dID
     * @return
     */
    @GetMapping("/findById/{id}")
    public ResultVo finnById(@PathVariable("id") String dID){
        DormitoryAdmin dormitoryAdmin=dormitoryAdminService.getById(dID);
        return ResultUtil.success(dormitoryAdmin);
    }

    /**
     * 根据id更新内容
     * @param admin
     * @return
     */
    @GetMapping("/upData")
    public ResultVo upData(DormitoryAdmin admin){
        boolean update = dormitoryAdminService.updateById(admin);
        if (update){
            return ResultUtil.success(0);
        }else{
            return ResultUtil.failed();
        }
    }

    /**
     * 删除宿管
     * @param dId
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public ResultVo deleteById(@PathVariable("id") String dId){
        boolean remove = dormitoryAdminService.removeById(dId);
        if (remove){
            return ResultUtil.success(0);
        }else{
            return ResultUtil.failed();
        }
    }

    /**
     * 获取宿管列表
     * -------------->楼宇添加
     * @return
     */
    @GetMapping("/adminList")
    public ResultVo adminList(){
        List<DormitoryAdmin> list = dormitoryAdminService.list();
        if (list!=null){
            return ResultUtil.success(list);
        }else return ResultUtil.failed();
    }
}

